package com.baizhi.cmfzdemo.controller;

import com.baizhi.cmfzdemo.entity.CmfzAlbum;
import com.baizhi.cmfzdemo.entity.CmfzArticle;
import com.baizhi.cmfzdemo.service.CmfzAlbumService;
import com.baizhi.cmfzdemo.service.CmfzArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * Created by 54433 on 2019/4/4.
 */
@Controller
@RequestMapping("/cmfzArticle")
public class CmfzArticleController {
    @Autowired
    private CmfzArticleService cmfzArticleService;

    //展示所有
    @RequestMapping("/queryAllCmfzArticle")
    @ResponseBody
    public Map queryAllCmfzArticle(int page,int rows){
        System.out.println(cmfzArticleService.queryAllCmfzArticle(page,rows));
        return cmfzArticleService.queryAllCmfzArticle(page,rows);
    }
    @RequestMapping("/deleteCmfzArticle")
    @ResponseBody
    public Map deleteCmfzArticle(int [] ids){
        return cmfzArticleService.deleteCmfzArticle(ids);
    }
    @RequestMapping("/insertCmfzAlbum")
    @ResponseBody
    public Map insertCmfzArticle(CmfzArticle cmfzArticle, MultipartFile articleImg, HttpServletRequest request) throws IOException {
        String fileName = articleImg.getOriginalFilename();
        cmfzArticle.setArticleImage("/img/"+fileName);
/*获取到upload的绝对路径*/
        String realPathNewMethod = request.getServletContext().getRealPath("img");
        articleImg.transferTo(new File(realPathNewMethod+"/"+fileName));
        return cmfzArticleService.addCmfzArticle(cmfzArticle);
    }
    @RequestMapping("/updateCmfzArticle")
    @ResponseBody
    public Map updateCmfzArticle(CmfzArticle cmfzArticle, MultipartFile articleImg, HttpServletRequest request) throws IOException {
        String fileName = articleImg.getOriginalFilename();
        cmfzArticle.setArticleImage("/img/"+fileName);
/*获取到upload的绝对路径*/
        String realPathNewMethod = request.getServletContext().getRealPath("img");
        articleImg.transferTo(new File(realPathNewMethod+"/"+fileName));
        return cmfzArticleService.updateCmfzArticle(cmfzArticle);
    }

}
